// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ExpandableQueryProvider.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.LinqKit
{
    #region Usings

    using System.Linq;
    using System.Linq.Expressions;

    #endregion

    /// <summary>
    /// The expandable query provider.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    internal class ExpandableQueryProvider<T> : IQueryProvider
    {
        #region Constants and Fields

        /// <summary>
        /// The query.
        /// </summary>
        private readonly ExpandableQuery<T> query;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ExpandableQueryProvider{T}"/> class.
        /// </summary>
        /// <param name="query">
        /// The query.
        /// </param>
        internal ExpandableQueryProvider(ExpandableQuery<T> query)
        {
            this.query = query;
        }

        #endregion

        // The following four methods first call ExpressionExpander to visit the expression tree, then call
        // upon the inner query to do the remaining work.
        #region Implemented Interfaces

        #region IQueryProvider

        /// <summary>
        /// The interface query provider. create query.
        /// </summary>
        /// <param name="expression">
        /// The expression.
        /// </param>
        /// <typeparam name="TElement">
        /// </typeparam>
        /// <returns>
        /// </returns>
        IQueryable<TElement> IQueryProvider.CreateQuery<TElement>(Expression expression)
        {
            return
                new ExpandableQuery<TElement>(this.query.InnerQuery.Provider.CreateQuery<TElement>(expression.Expand()));
        }

        /// <summary>
        /// The interface query provider. create query.
        /// </summary>
        /// <param name="expression">
        /// The expression.
        /// </param>
        /// <returns>
        /// </returns>
        IQueryable IQueryProvider.CreateQuery(Expression expression)
        {
            return this.query.InnerQuery.Provider.CreateQuery(expression.Expand());
        }

        /// <summary>
        /// The interface query provider. execute.
        /// </summary>
        /// <param name="expression">
        /// The expression.
        /// </param>
        /// <typeparam name="TResult">
        /// </typeparam>
        /// <returns>
        /// </returns>
        TResult IQueryProvider.Execute<TResult>(Expression expression)
        {
            return this.query.InnerQuery.Provider.Execute<TResult>(expression.Expand());
        }

        /// <summary>
        /// The interface query provider. execute.
        /// </summary>
        /// <param name="expression">
        /// The expression.
        /// </param>
        /// <returns>
        /// The interface query provider. execute.
        /// </returns>
        object IQueryProvider.Execute(Expression expression)
        {
            return this.query.InnerQuery.Provider.Execute(expression.Expand());
        }

        #endregion

        #endregion
    }
}